local file = "class.lua"
local newc = require("newc")
local ocall = reload("ocall")
local classgc = reload("classgc")
---@type table<table,table>
local methods = newc(file, "methods")

---@generic T
---@param super? T  -- 可选的基类
---@return T       -- 返回增强后的类
return function (super)
    local info = debug.getinfo(2, "S")
    local file = info.source

    ---@class declare @class类型
    local declare = newc(file, "declare")
    declare.__base = super
    declare.__type = 'class'



    local virtual = newc(file, "virtual")
    methods[declare] = virtual

    if super then
        local supermeta = newc(file, "supermeta")
        supermeta.__index = methods[super]
        setmetatable(virtual, supermeta)
    end

    declare.new = function (...)
        local ometa = newc(file, "object")
        ometa.__index = virtual
        ometa.__gc = classgc
        
        local obj = {
            __base = declare,
            __type = 'object',
            __file = file,
        }
        setmetatable(obj, ometa)
        obj:ctor(...)
        return obj
    end

    local meta = newc(file, "meta")
    meta.__index = virtual
    meta.__newindex = virtual
    ---简化new方法
    meta.__call = ocall
    setmetatable(declare, meta)
    return declare
end
